package com.example.summary.leetcode;

import com.alibaba.fastjson.JSONObject;

/**
 * Created by wzn on 2019/5/24.
 */
public class LeetCode226 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {

        TreeNode g = new TreeNode(2, null, null);
        TreeNode h = new TreeNode(4, null, null);
        TreeNode i = new TreeNode(8, null, null);

        TreeNode d = new TreeNode(1, null, g);
        TreeNode e = new TreeNode(5, h, null);
        TreeNode f = new TreeNode(7, null, i);

        TreeNode b = new TreeNode(3, d, e);
        TreeNode c = new TreeNode(9, f, null);

        TreeNode a = new TreeNode(6, b, c);

        invertTree(a);

        System.out.println(a);

    }

    public static TreeNode invertTree(TreeNode root) {
        if (null == root) {
            return null;
        }

        TreeNode temp = root.left;

        root.left = root.right;
        root.right = temp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }

}
